package com.banciyuan.bcywebview.biz.main.discover.newestwork;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.activity.BaseActivity;
import com.banciyuan.bcywebview.biz.main.discover.top.TopFragment;
import com.banciyuan.bcywebview.biz.main.discover.topwork.TopWorkActivity;
import com.banciyuan.bcywebview.utils.intent.gotoUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by squall on 2017/5/23.
 */

public class NewestWorkActivity extends BaseActivity implements View.OnClickListener {

    protected TabLayout mTabLayout;
    private ViewPager mViewPager;
    private String[] topCreatorNames;
    private List<Fragment> mTopFragmentList = new ArrayList<>();
    private int tabType;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.top_user_activity);
        initArgs();
        getFragment();
        initActionbar();
        initUi();
    }

    @Override
    protected void initArgs() {
        tabType = getIntent().getIntExtra(gotoUtil.INTENT_VALUE_ONE, TopFragment.ILLUST_TAG);
        if (tabType == TopFragment.ILLUST_TAG) {
            topCreatorNames = new String[]{getString(R.string.newest_dongjin), getString(R.string.newest_creat)};
        } else if (tabType == TopFragment.WRITER_TAG) {
            topCreatorNames = new String[]{getString(R.string.newest_dongjin), getString(R.string.newest_creat)};
        } else if (tabType == TopFragment.COS_TAG) {
            topCreatorNames = new String[]{getString(R.string.newest_prowork), getString(R.string.newest_perwork)};
        }
    }


    @Override
    protected void initActionbar() {
        TextView tvActionTitle = (TextView) findViewById(R.id.base_action_bar_title);
        findViewById(R.id.base_action_bar_home).setOnClickListener(this);
        tvActionTitle.setText(getString(R.string.newchoose));
    }

    @Override
    protected void initUi() {
        mTabLayout = (TabLayout) findViewById(R.id.tablayout);
        mViewPager = (ViewPager) findViewById(R.id.mViewpager);
        mViewPager.setOffscreenPageLimit(2);
        mViewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return mTopFragmentList.get(position);
            }

            @Override
            public int getCount() {
                return mTopFragmentList.size();
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return topCreatorNames[position];
            }
        });
        mTabLayout.setupWithViewPager(mViewPager);
    }

    private void getFragment() {
        if (tabType == TopFragment.WRITER_TAG) {
            for (int i = 0; i < 2; i++) {
                NewestNovelWorkFragment topFragment = new NewestNovelWorkFragment();
                Bundle bundle = new Bundle();
                if (i == 0) {
                    bundle.putString(gotoUtil.INTENT_VALUE_TWO, "fanfic");
                } else {
                    bundle.putString(gotoUtil.INTENT_VALUE_TWO, "fiction");
                }
                topFragment.setArguments(bundle);
                mTopFragmentList.add(topFragment);
            }
        } else {
            for (int i = 0; i < 2; i++) {
                NewestWorkFragment topFragment = new NewestWorkFragment();
                Bundle bundle = new Bundle();
                if (tabType == TopFragment.ILLUST_TAG) {
                    bundle.putInt(gotoUtil.INTENT_VALUE_ONE, TopWorkActivity.ILLUST_TYPE);
                } else if (tabType == TopFragment.COS_TAG) {
                    bundle.putInt(gotoUtil.INTENT_VALUE_ONE, TopWorkActivity.COS_TYPE);
                }
                if (i == 0) {
                    if (tabType == TopFragment.ILLUST_TAG) {
                        bundle.putString(gotoUtil.INTENT_VALUE_TWO, "fanart");
                    } else if (tabType == TopFragment.COS_TAG) {
                        bundle.putString(gotoUtil.INTENT_VALUE_TWO, "work");
                    }
                }
                if (i == 1) {
                    if (tabType == TopFragment.ILLUST_TAG) {
                        bundle.putString(gotoUtil.INTENT_VALUE_TWO, "artwork");
                    } else if (tabType == TopFragment.COS_TAG) {
                        bundle.putString(gotoUtil.INTENT_VALUE_TWO, "prework");
                    }
                }
                topFragment.setArguments(bundle);
                mTopFragmentList.add(topFragment);
            }
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.base_action_bar_home:
                finish();
                break;
        }
    }
}
